#ifndef THIRDPARTY_LOGGING_ASYNC_LOGGING_H_
#define THIRDPARTY_LOGGING_ASYNC_LOGGING_H_
#pragma once

#include "thirdparty/logging/logger.h"
#include "thirdparty/logging/fixed_buffer.h"
#include <thread>
#include <mutex>
#include <memory>
#include <condition_variable>
#include <vector>
namespace logging {

class AsyncLogging {
 public:
  typedef FixedBuffer<4000000> LargeBuffer;
  typedef std::unique_ptr<LargeBuffer> LargeBufferPtr;

  AsyncLogging(const std::string& base_dir);
  ~AsyncLogging() {
    running_ = false;
    buffers_no_empty_cond_.notify_all();
    async_excutor_.join();
  }
  void ThreadFunction();
  void OutputLog(const char* data, uint32_t len);

 private:
  bool running_ = false;
  LargeBufferPtr current_buffer_;
  LargeBufferPtr backend_buffer_;
  std::vector<LargeBufferPtr> buffers_;
  std::vector<LargeBufferPtr> write_buffers_;
  std::condition_variable buffers_no_empty_cond_;
  mutable std::mutex buf_mutex_;
  // flush every 3 seconds
  const uint64_t kFlushInterval = 3;
  std::thread async_excutor_;

};

}

#endif  // THIRDPARTY_LOGGING_ASYNC_LOGGING_H_
